package com.sl.au.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.sl.au.entity.ContractProductInfoView;
import com.sl.au.entity.User;
import com.sl.au.service.GetProductChartService;
import com.sl.au.service.QueryService;
@Service("GetProductChartService")
@Transactional
public class GetProductChartServiceImpl implements GetProductChartService{
	
	@PersistenceContext
	private EntityManager em;
	
	@Autowired
	QueryService queryService;

	@Override
	public List getProductChartsService(int queryType, String beginDate, String endDate, String type) {
		// TODO Auto-generated method stub
		return this.em.createNativeQuery("{call getProductChart(?,?,?,?)}")
				.setParameter(1, queryType)
				.setParameter(2, beginDate)
				.setParameter(3, endDate == null ? "" : endDate)
				.setParameter(4, type == null ? "" : type)
				.getResultList();
	}

	@Override
	public List<ContractProductInfoView> getProductTotal(int start, int length, Map<String, Object> condition,
			User user, String orderName, String dir) {
		// TODO Auto-generated method stub
		@SuppressWarnings("unchecked")
		HashMap<String, Object>numMap=(HashMap<String, Object>)condition.get("numMap");
		Map<String,Object>namedParamsMap=new HashMap<String,Object>();
		StringBuilder hql=queryService.buildCombinedQueryHql("ContractProductInfoView",null, condition, namedParamsMap,null, user);
		for(String key:numMap.keySet()){
			Object value=numMap.get(key);
			if(key=="nUnitPrice"){
				String[] a = value.toString().split(",");
				hql.append(" and ( 1<>1 ");
				for(int i=0;i<a.length;i++){
					if(a[i].equals("'100万以下'")) hql.append(" or (e.nUnitPrice>=0 and nUnitPrice<=1000000  )");
					if(a[i].equals("'100-500万'")) hql.append(" or (e.nUnitPrice >1000000 and nUnitPrice <=5000000 )");
					if(a[i].equals("'500-1000万'")) hql.append(" or (e.nUnitPrice >5000000 and nUnitPrice <= 10000000 )");
					if(a[i].equals("'1000万以上'")) hql.append(" or (e.nUnitPrice >10000000  )");
				}
				hql.append(") "); 
			}else if(key=="supplierCode"){
				hql.append("and (e."+key+" like '%"+value.toString()+"%')");
			}
			else if(key == "orderUnit"){
				hql.append("and (e."+key+" like '%"+value.toString()+"%')");
			}
			else{
				if (value instanceof String){
					 if(StringUtils.hasText((String)value)){
						 System.out.println(key+" + "+value.toString());
						 hql.append("and (e."+key+" in ("+value.toString()+"))");
					 }
				}
				
				
			}
		}
		String dataFilter=condition.get("dataFilter").toString();
		if(StringUtils.hasText(dataFilter)){
			//this.contractDataFilter(hql, dataFilter, user);
		}else{
			//return null;
		}
		hql.append(" order by " + orderName + " " + dir);
		System.out.println(hql.toString());
		Query query=queryService.createQueryHql(hql.toString());
		query.setFirstResult(start).setMaxResults(length);
		return queryService.executeHql(query, namedParamsMap);
	}

}
